package com.yanfan.energy.controller;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.energy.domain.SaveMoneyQueryByArea;
import com.yanfan.energy.domain.SaveMoneyStandard;
import com.yanfan.energy.domain.SaveMoneyStandardInfo;
import com.yanfan.energy.service.SaveMoneyStandardService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;

/**
 * (SaveMoneyStandard)表控制层
 *
 * @author makejava
 * @since 2023-05-08 16:48:44
 */
@RestController
@RequestMapping("/saveMoneyStandard")
@Api(tags = "省钱管理配置相关")
public class SaveMoneyStandardController {


    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 服务对象
     */
    @Resource
    private SaveMoneyStandardService saveMoneyStandardService;

    /**
     * 分页查询
     * 查询条件
     *
     * @param saveMoneyStandard 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public AjaxResult queryByPage(SaveMoneyStandard saveMoneyStandard) {
        return AjaxResult.success(this.saveMoneyStandardService.queryList(saveMoneyStandard));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        return AjaxResult.success(this.saveMoneyStandardService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param saveMoneyStandard 实体
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody SaveMoneyStandard saveMoneyStandard) {
        return AjaxResult.success(this.saveMoneyStandardService.insert(saveMoneyStandard));
    }

    /**
     * 编辑数据
     *
     * @param saveMoneyStandard 实体
     * @return 编辑结果
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SaveMoneyStandard saveMoneyStandard) {
        return AjaxResult.success(this.saveMoneyStandardService.update(saveMoneyStandard));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public AjaxResult deleteById(Long id) {
        return AjaxResult.success(this.saveMoneyStandardService.deleteById(id));
    }

    @ApiOperation("省钱标准初始化接口")
    @PostMapping("/saveMoneyInit")
    public AjaxResult saveMoneyInit(@RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime, @RequestParam("purchaseSign") int purchaseSign, @RequestParam("groupByStr") String groupByStr) {
        this.saveMoneyStandardService.saveMoneyInit(startTime, endTime, purchaseSign, groupByStr);
        return AjaxResult.success();
    }

    @ApiOperation("每个月1号凌晨执行一次")
    @PostMapping("/saveMoneyTimer")
    public AjaxResult saveMoneyTimer(@RequestParam("purchaseSign") int purchaseSign, @RequestParam("groupByStr") String groupByStr) {
        this.saveMoneyStandardService.saveMoneyTimer(purchaseSign, groupByStr);
        return AjaxResult.success();
    }


    /**
     * 省钱设备标准列表查询
     *
     * @return
     */
    @ApiOperation("省钱设备标准列表查询")
    @PostMapping("/saveMoneyStandardDeviceList")
    public AjaxResult saveMoneyStandardDeviceList(@RequestBody SaveMoneyStandard saveMoneyStandard) {
        PageHelper.startPage(saveMoneyStandard.getPageNum(), saveMoneyStandard.getPageSize());
        return AjaxResult.success(new PageInfo<>(saveMoneyStandardService.saveMoneyStandardDeviceList(saveMoneyStandard)));
    }


    /**
     * 省钱采购标准列表查询
     *
     * @return
     */
    @ApiOperation("省钱采购标准列表查询")
    @PostMapping("/saveMoneyStandardPurchaseList")
    public AjaxResult saveMoneyStandardPurchaseList(@RequestBody SaveMoneyStandard saveMoneyStandard) {
        return AjaxResult.success(saveMoneyStandardService.saveMoneyStandardPurchaseList(saveMoneyStandard));
    }


    /**
     * 设备省钱标准修改
     *
     * @return
     */
    @ApiOperation("设备省钱标准修改")
    @PostMapping("/saveMoneyStandardDeviceEdit")
    public AjaxResult saveMoneyStandardDeviceEdit(@RequestBody SaveMoneyStandardInfo saveMoneyStandardInfo) {
        return AjaxResult.success(saveMoneyStandardService.saveMoneyStandardDeviceEdit(saveMoneyStandardInfo));
    }


    /**
     * 省钱标准详情
     *
     * @return
     */
    @ApiOperation("省钱标准详情")
    @PostMapping("/saveMoneyStandardDeviceDetail")
    public AjaxResult saveMoneyStandardDeviceDetail(@RequestBody SaveMoneyStandardInfo saveMoneyStandardInfo) {
        SaveMoneyStandardInfo resultData = saveMoneyStandardService.saveMoneyStandardDeviceDetail(saveMoneyStandardInfo);
        if (resultData != null) {
            return AjaxResult.success(resultData);
        }
        AjaxResult success = AjaxResult.success(resultData);
        success.put("data", null);
        return success;
    }


    /**
     * 编辑采购标准
     *
     * @return
     */
    @ApiOperation("设备省钱标准 根据区域维度查省钱标准")
    @PostMapping("/saveMoneyQueryByArea")
    public AjaxResult saveMoneyQueryByArea(@RequestBody SaveMoneyQueryByArea saveMoneyQueryByArea) {
        return AjaxResult.success(saveMoneyStandardService.saveMoneyQueryByArea(saveMoneyQueryByArea));
    }

    /**
     * 区域维度列表查询
     *
     * @return
     */
    @ApiOperation("区域维度列表查询")
    @PostMapping("/areaList")
    public AjaxResult areaList(@RequestBody SaveMoneyQueryByArea saveMoneyQueryByArea) {
        return AjaxResult.success(saveMoneyStandardService.areaList(saveMoneyQueryByArea));
    }


}

